package com.joeyee.echartsgenerator.config;

import com.joeyee.echartsgenerator.enums.EchartsType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;

/**
 * @author Joe Yee
 * @version 1.0
 * @description: 字符转换枚举转换器
 * @date 2021/8/10 10:28
 */
//@Component
@Slf4j
public class EchartsTypeConverter implements Converter<String, EchartsType> {

    @Override
    public EchartsType convert(String s) {
        log.info("Parameter is: " + s);
        return (EchartsType)EchartsTypeConverter.getEnum(EchartsType.class, s);
    }

    public static <T extends EchartsType> Object getEnum(Class<T> targetType, String source){
        for (T enumObj : targetType.getEnumConstants()){
            if(source.equals(String.valueOf(enumObj.getId()))){
                return enumObj;
            }
        }
        return null;
    }

}
